﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace GraphicSplit101
{
    public partial class UCInspectionArea : UserControl
    {
        private Bitmap bitmap = GraphicSplit101.Properties.Resources.Sample;

        public Bitmap Bitmap
        {
            get { return bitmap; }
            set { bitmap = value; }
        }

        private int pid = 1;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }

        public UCInspectionArea()
        {
            InitializeComponent();
            pictureBox1.Image = Bitmap;
            pictureBox2.Image = Bitmap;            
            
        }

        private Point start = new Point();//矩形起点
        private Point end = new Point();//矩形终点
        private bool blnDraw = false;//是否开始画矩形
        private bool hasDraw = false;   //记录是否已画检测区域

        private Point start2 = new Point();//矩形起点
        private Point end2 = new Point();//矩形终点
        private bool blnDraw2 = false;//是否开始画矩形
        private bool hasDraw2 = false;   //记录是否已画检测区域
        private bool blnLoading = true; //是否是界面刚加载时

        Graphics g;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            g = this.pictureBox1.CreateGraphics();
            start.X = e.X;
            start.Y = e.Y;
            end.X = e.X;
            end.Y = e.Y;
            blnDraw = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            g.DrawRectangle(new Pen(Color.Blue), start.X, start.Y, e.X - start.X, e.Y - start.Y);
            blnDraw = false;
            if (end.X > start.X && end.Y > start.Y)
            {
                float XRadio = (float)pictureBox1.Image.Width / pictureBox1.Width;
                float YRadio = (float)pictureBox1.Image.Height / pictureBox1.Height;

                int count = Convert.ToInt32(DBUtils.GetUniqueValue("select count(1) from InspectionAreaDetail where [type]=2 and [pid]=" + Pid));
                if (count > 0)
                {
                    DBUtils.ExecuteSql("update InspectionAreaDetail set [Top]=" + start.Y * YRadio + ", [Left]=" + start.X * XRadio + ",Width=" + (end.X - start.X) * XRadio + ",Height=" + (end.Y - start.Y) * YRadio + " where [type]=2 and [pid]=" + Pid);
                }
                else
                {
                    DBUtils.ExecuteSql("insert into InspectionAreaDetail([pid],[type],[Top],[Left],[Width],[Height]) values(" + Pid + ",2," + start.Y * YRadio + ", " + start.X * XRadio + "," + (end.X - start.X) * XRadio + "," + (end.Y - start.Y) * YRadio + ")");
                }
                hasDraw = true;
            }
            else
            {
                DBUtils.ExecuteSql("delete from InspectionAreaDetail where [pid]=" + Pid);
                hasDraw = false;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (blnDraw)
            {
                //先擦除
                pictureBox1.Refresh();
                end.X = e.X;
                end.Y = e.Y;
                //再画
                g.DrawRectangle(new Pen(Color.Blue), start.X, start.Y, end.X - start.X, end.Y - start.Y);
            }
        }


        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = tabControl1.SelectedIndex;
            if (i == 0)
            {
                g = this.pictureBox1.CreateGraphics();
                pictureBox1.Refresh();
                drawPictureBox1();
            }
            else if (i == 1)
            {
                g = this.pictureBox2.CreateGraphics();
                pictureBox2.Refresh();
                drawPictureBox1();
                if (draw2PointsList.Count > 0)
                {
                    drawPictureBox2();
                }
            }
        }

        private List<Point[]> draw2PointsList = new List<Point[]>();

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            g = this.pictureBox2.CreateGraphics();
            start2.X = e.X;
            start2.Y = e.Y;
            end2.X = e.X;
            end2.Y = e.Y;
            blnDraw2 = true;
        }

        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            
            blnDraw2 = false;
            if (end2.X > start2.X && end2.Y > start2.Y)
            {
                float XRadio = (float)pictureBox2.Image.Width / pictureBox2.Width;
                float YRadio = (float)pictureBox2.Image.Height / pictureBox2.Height;

                DBUtils.ExecuteSql("insert into InspectionAreaDetail([pid],[type],[Top],[Left],[Width],[Height]) values(" + Pid + ",3," + start2.Y * YRadio + ", " + start2.X * XRadio + "," + (end2.X - start2.X) * XRadio + "," + (end2.Y - start2.Y) * YRadio + ")");
                hasDraw = true;
                draw2PointsList.Add(new Point[] { new Point(start2.X, start2.Y), new Point(end2.X, end2.Y) });
            }
            drawPictureBox2();
            drawPictureBox1();
        }

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (blnDraw2)
            {
                //先擦除
                pictureBox2.Refresh();
                end2.X = e.X;
                end2.Y = e.Y;
                //再画
                g.DrawRectangle(new Pen(Color.Red), start2.X, start2.Y, end2.X - start2.X, end2.Y - start2.Y);
                drawPictureBox2();
            }
            drawPictureBox1();
        }

        private void drawPictureBox1()
        {
            if (hasDraw)
            {
                g.DrawRectangle(new Pen(Color.Blue), start.X, start.Y, end.X - start.X, end.Y - start.Y);
            }
        }

        private void drawPictureBox2()
        {
            foreach (Point[] points in draw2PointsList)
            {
                g.DrawRectangle(new Pen(Color.Red), points[0].X, points[0].Y, points[1].X - points[0].X, points[1].Y - points[0].Y);
            }
        }

        private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int x = e.X;
            int y = e.Y;

            var pointsList = from item in draw2PointsList
                             where item[0].X <= x && item[1].X >= x
                             && item[0].Y <= y && item[1].Y >= y
                             select item;
            for (int i = 0; i < pointsList.Count(); i++)
            {
                Point[] points = pointsList.ElementAt(i);
                draw2PointsList.Remove(points);
                float XRadio = (float)pictureBox2.Image.Width / pictureBox2.Width;
                float YRadio = (float)pictureBox2.Image.Height / pictureBox2.Height;
                DBUtils.ExecuteSql("delete from InspectionAreaDetail where [pid]=" + Pid + " and [Type]=3 and [Top]=" + (int) (points[0].Y * YRadio) + " and  [Left]=" + (int) (points[0].X * XRadio));
            }
            pictureBox2.Refresh();
            drawPictureBox2();
        }

        private void UCInspectionArea_Paint(object sender, PaintEventArgs e)
        {            
            if (blnLoading)
            {
                DataTable dt = DBUtils.GetDataTable("select * from InspectionAreaDetail where [type]=2 and [pid]=" + Pid);
                if (dt.Rows.Count > 0)
                {
                    float XRadio = (float)pictureBox1.Image.Width / pictureBox1.Width;
                    float YRadio = (float)pictureBox1.Image.Height / pictureBox1.Height;
                    hasDraw = true;
                    DataRow dr = dt.Rows[0];
                    start.X = Convert.ToInt32(Convert.ToInt32(dr["Left"]) / XRadio);
                    start.Y = Convert.ToInt32(Convert.ToInt32(dr["Top"]) / YRadio);
                    end.X = start.X + Convert.ToInt32(Convert.ToInt32(dr["Width"]) / XRadio);
                    end.Y = start.Y + Convert.ToInt32(Convert.ToInt32(dr["Height"]) / YRadio);
                }
                dt = DBUtils.GetDataTable("select * from InspectionAreaDetail where [type]=3 and [pid]=" + Pid);
                foreach (DataRow dr in dt.Rows)
                {
                    float XRadio = (float)pictureBox2.Image.Width / pictureBox2.Width;
                    float YRadio = (float)pictureBox2.Image.Height / pictureBox2.Height;
                    int left = Convert.ToInt32(Convert.ToInt32(dr["Left"]) / XRadio);
                    int top = Convert.ToInt32(Convert.ToInt32(dr["Top"]) / YRadio);
                    draw2PointsList.Add(new Point[] { new Point(left, top), 
                    new Point(left + Convert.ToInt32(Convert.ToInt32(dr["Width"]) / XRadio), top + Convert.ToInt32(Convert.ToInt32(dr["Height"]) / YRadio)) });
                }

                pictureBox1.Refresh();
                g = this.pictureBox1.CreateGraphics();
                drawPictureBox1();
                blnLoading = false;
            }
        }

    }
}
